package net.siufung.market.base.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.user.controller.model.dto.UserUpdateInfoDto;
import net.siufung.market.base.user.mapper.entity.User;
import net.siufung.market.base.user.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 陈建峰
 * @date 2023/2/12 12:10 PM
 * @since V1.0.0
 */


@Api(tags = "用户密码管理")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/user/info")
public class UserInfoController {

    private final IUserService userService;

    /**
     * 更新 系统用户表
     * @param userUpdateInfoDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统用户表 信息")
    @PostMapping(value="/update")
    public Result<Void> updateUserInfo(
            @RequestBody UserUpdateInfoDto userUpdateInfoDto) {
        userUpdateInfoDto.validated();
        User userOf = userService.getById(userUpdateInfoDto.getId());
        CopyUtil.copyPropertiesIgnoreNull(userUpdateInfoDto.to(), userOf);
        if(userService.updateById(userOf, userUpdateInfoDto.toDetail())){
            return Result.ok(ResultOk.UPDATE);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }
}
